<template>
    <div class="app-container">
        <el-input
            v-model="filename"
            placeholder="Please enter the file name (default excel-list)"
            style="width:350px;"
            prefix-icon="el-icon-document"
        />
        <el-button
            :loading="downloadLoading"
            style="margin-bottom:20px"
            type="primary"
            icon="el-icon-document"
            @click="handleDownload"
        >
            Export Selected Items
        </el-button>
        <a
            href="https://panjiachen.github.io/vue-element-admin-site/feature/component/excel.html"
            target="_blank"
            style="margin-left:15px;"
        >
            <el-tag type="info">Documentation</el-tag>
        </a>
        <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list"
            element-loading-text="拼命加载中"
            border
            fit
            highlight-current-row
            @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" align="center" />
            <el-table-column align="center" label="Id" width="95">
                <template slot-scope="scope">
                    {{ scope.$index }}
                </template>
            </el-table-column>
            <el-table-column label="Title">
                <template slot-scope="scope">
                    {{ scope.row.title }}
                </template>
            </el-table-column>
            <el-table-column label="Author" width="110" align="center">
                <template slot-scope="scope">
                    <el-tag>{{ scope.row.author }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="Readings" width="115" align="center">
                <template slot-scope="scope">
                    {{ scope.row.pageviews }}
                </template>
            </el-table-column>
            <el-table-column align="center" label="PDate" width="220">
                <template slot-scope="scope">
                    <i class="el-icon-time" />
                    <span>{{ scope.row.display_time }}</span>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
function fetchList() {
    return new Promise((resolve, reject) => {
        resolve({
            data: {
                items: [
                    {
                        id: 'id',
                        title: 'title',
                        author: 'author',
                        pageviews: 'pageviews',
                        display_time: 'display_time'
                    },
                    {
                        id: 'id',
                        title: 'title',
                        author: 'author',
                        pageviews: 'pageviews',
                        display_time: 'display_time'
                    },
                    {
                        id: 'id',
                        title: 'title',
                        author: 'author',
                        pageviews: 'pageviews',
                        display_time: 'display_time'
                    },
                    {
                        id: 'id',
                        title: 'title',
                        author: 'author',
                        pageviews: 'pageviews',
                        display_time: 'display_time'
                    },
                    {
                        id: 'id',
                        title: 'title',
                        author: 'author',
                        pageviews: 'pageviews',
                        display_time: 'display_time'
                    }
                ]
            }
        });
    });
}
export default {
    name: 'ExcelJsonDownload',
    data() {
        return {
            list: null,
            listLoading: true,
            multipleSelection: [],
            downloadLoading: false,
            filename: ''
        };
    },
    created() {
        this.fetchData();
    },
    methods: {
        fetchData() {
            this.listLoading = true;
            fetchList(this.listQuery).then(response => {
                this.list = response.data.items;
                this.listLoading = false;
            });
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        handleDownload() {
            if (this.multipleSelection.length) {
                this.downloadLoading = true;
                import('@/vendor/Export2Excel').then(excel => {
                    const tHeader = ['Id', 'Title', 'Author', 'Readings', 'Date'];
                    const filterVal = ['id', 'title', 'author', 'pageviews', 'display_time'];
                    const list = this.multipleSelection;

                    const data = this.formatJson(filterVal, list);

                    console.log('data', data);
                    console.log('list', list);
                    let beforeData = [
                        ['支付宝交易记录明细查询'],
                        ['账号:[18230275891]'],
                        ['起始日期:[2020-10-22 00:00:00]   终止日期:[2020-10-28 16:23:09]'],
                        ['---------------------------------交易记录明细列表------------------------------------']
                    ];
                    let afterData = [
                        ['------------------------------------------------------------------------------------']
                    ];

                    beforeData = this.formatDataFillStr(beforeData, tHeader);
                    afterData = this.formatDataFillStr(afterData, tHeader);

                    excel.export_json_to_excel({
                        beforeData,
                        afterData,
                        header: tHeader,
                        data,
                        filename: this.filename,
                        bookType: 'csv'
                    });
                    this.$refs.multipleTable.clearSelection();
                    this.downloadLoading = false;
                });
            } else {
                this.$message({
                    message: 'Please select at least one item',
                    type: 'warning'
                });
            }
        },
        formatJson(filterVal, jsonData) {
            return jsonData.map(v => filterVal.map(j => v[j]));
        },
        // 把beforeData， afterData数组补充 空字符串
        formatDataFillStr(data, tHeader) {
            const maxLength = Math.max(tHeader.length, ...data.map(item => item.length));

            return data.map(item => new Array(maxLength).fill('').map((emptyStr, index) => item[index] || emptyStr));
        }
    }
};
</script>
